package example.com.zcq.mymj.net;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * author：Anumbrella
 * Date：16/5/29 下午3:40
 * 单例模式
 */
public class RetrofitHttp {

    private static HttpInterface singleton;

    /**
     * 获取接口
     *
     * @param client
     * @return
     */
    public static HttpInterface getRetrofit(OkHttpClient client) {
        if (singleton == null) {
            synchronized (RetrofitHttp.class) {
                singleton = createRetrofit(client).create(HttpInterface.class);
            }
        }
        return singleton;
    }

    //不用传OkHttpClient
    public static HttpInterface getSimpleRetrofit() {
        if (singleton == null) {
            synchronized (RetrofitHttp.class) {
                singleton = createRetrofit(new OkHttpClient.Builder().build()).create(HttpInterface.class);
            }
        }
        return singleton;
    }

    /**
     * 创建retrofit对象
     *
     * @param client
     * @return
     */
    private static Retrofit createRetrofit(OkHttpClient client) {
        //        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        //        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://v.juhe.cn/")
                //                .baseUrl("http://route.showapi.com/")
                .addConverterFactory(GsonConverterFactory.create())
                //                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        return retrofit;
    }


}
